package com.nk.system.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nk.system.beans.UserScoreBean;
import com.nk.system.common.search.Search;
import com.nk.system.dao.UserScoreDao;
import com.nk.system.domain.UserScore;
import com.nk.system.service.RegisterService;

@Service("registerService")
public class RegisterServiceImpl extends GenericServiceImpl<UserScore, Integer, UserScoreDao>  implements RegisterService{
	
	private Logger log = Logger.getLogger(RegisterServiceImpl.class);

	@Autowired
	public RegisterServiceImpl(UserScoreDao userScoreDao) {
		super(userScoreDao);
	}
	
	UserScoreBean userscoreBean = null;

	@Override
	public UserScoreBean getScore(int id) {
		// TODO Auto-generated method stub
		
		try {
			Search search = new Search(UserScore.class);
			search.addFilterEqual("userid", id);
			UserScore userScore = dao.searchUnique(search);
		
			if(userScore != null) {
				userscoreBean = new UserScoreBean();
				userscoreBean.setGpax(userScore.getGpax());
				userscoreBean.setThai(userScore.getThai());
				userscoreBean.setSocial(userScore.getSocial());
				userscoreBean.setEng(userScore.getEng());
				userscoreBean.setMath(userScore.getMath());
				userscoreBean.setScience(userScore.getScience());
				userscoreBean.setSport(userScore.getSport());
				userscoreBean.setArt(userScore.getArt());
				userscoreBean.setTechnology(userScore.getTechnology());
				userscoreBean.setGat(userScore.getGat());
				userscoreBean.setPat1(userScore.getPat1());
				userscoreBean.setPat2(userScore.getPat2());
				userscoreBean.setPat3(userScore.getPat3());
				userscoreBean.setPat4(userScore.getPat4());
				userscoreBean.setPat5(userScore.getPat5());
				userscoreBean.setPat6(userScore.getPat6());
				userscoreBean.setPat71(userScore.getPat71());
				userscoreBean.setPat72(userScore.getPat72());
				userscoreBean.setPat73(userScore.getPat73());
				userscoreBean.setPat74(userScore.getPat74());
				userscoreBean.setPat75(userScore.getPat75());
				userscoreBean.setPat76(userScore.getPat76());
			}
		}
		catch(Exception e) {
			log.error(e,e);
		}
		
		return userscoreBean;
	}

	@Override
	public void setScore(int id, double gpax, int thai, int social,
			int eng, int math, int science, int sport, int art, int tech,
			int gat, int pat1, int pat2, int pat3, int pat4, int pat5,
			int pat6, int pat71, int pat72, int pat73, int pat74, int pat75,
			int pat76) {
		// TODO Auto-generated method stub
		
		Search search = new Search(UserScore.class);
		search.addFilterEqual("userid", id);
		UserScore userScore = dao.searchUnique(search);
		
		userScore.setGpax(gpax);
		userScore.setThai(thai);
		userScore.setSocial(social);
		userScore.setEng(eng);
		userScore.setMath(math);
		userScore.setScience(science);
		userScore.setSport(sport);
		userScore.setArt(art);
		userScore.setTechnology(tech);
		userScore.setGat(gat);
		userScore.setPat1(pat1);
		userScore.setPat2(pat2);
		userScore.setPat3(pat3);
		userScore.setPat4(pat4);
		userScore.setPat5(pat5);
		userScore.setPat6(pat6);
		userScore.setPat71(pat71);
		userScore.setPat72(pat72);
		userScore.setPat73(pat73);
		userScore.setPat74(pat74);
		userScore.setPat75(pat75);
		userScore.setPat76(pat76);
		
		
		/*dao.save(userScore);*/
		dao.update(userScore);
		
		/**
		 * Insert data
		 *//*
		Program program = new Program();
		program.setAddress("aaa");
		dao.save(program);
		
		dao.update(program);*/
	}

	@Override
	public void insert(int userId) {
		// TODO Auto-generated method stub
		UserScore userscore = new UserScore();
		
		userscore.setUserid(userId);
		userscore.setGpax(0);
		userscore.setThai(0);
		userscore.setSocial(0);
		userscore.setEng(0);
		userscore.setMath(0);
		userscore.setScience(0);
		userscore.setSport(0);
		userscore.setArt(0);
		userscore.setTechnology(0);
		userscore.setGat(0);
		userscore.setPat1(0);
		userscore.setPat2(0);
		userscore.setPat3(0);
		userscore.setPat4(0);
		userscore.setPat5(0);
		userscore.setPat6(0);
		userscore.setPat71(0);
		userscore.setPat72(0);
		userscore.setPat73(0);
		userscore.setPat74(0);
		userscore.setPat75(0);
		userscore.setPat76(0);
		
		dao.save(userscore);
	}

	
}
